const getBrowserInfo = function () {
  let agent = navigator.userAgent.toLowerCase();
  let regStrIe = /msie [\d.]+;/gi;
  let regStrFf = /firefox\/[\d.]+/gi;
  let regStrChrome = /chrome\/[\d.]+/gi;
  let regStrSaf = /safari\/[\d.]+/gi;
  if (agent.indexOf("msie") > 0) {
    return "浏览器内核：" + agent.match(regStrIe);
  }
  // firefox
  if (agent.indexOf("firefox") > 0) {
    return "浏览器内核：" + agent.match(regStrFf);
  }
  // Chrome
  if (agent.indexOf("chrome") > 0) {
    return "浏览器内核：" + agent.match(regStrChrome);
  }
  // Safari
  if (agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0) {
    return "浏览器内核：" + agent.match(regStrSaf);
  }
};
const detectOS = function () {
  let sUserAgent = navigator.userAgent;
  let isWin =
    navigator.platform === "Win32" || navigator.platform === "Windows";
  let isMac =
    navigator.platform === "Mac68K" ||
    navigator.platform === "MacPPC" ||
    navigator.platform === "Macintosh" ||
    navigator.platform === "MacIntel";
  if (isMac) return "Mac";
  let isUnix = navigator.platform === "X11" && !isWin && !isMac;
  if (isUnix) return "Unix";
  let isLinux = String(navigator.platform).indexOf("Linux") > -1;
  let bIsAndroid = sUserAgent.toLowerCase().match(/android/i) === "android";
  if (isLinux) {
    if (bIsAndroid) return "Android";
    else return "Linux";
  }
  if (isWin) {
    let isWin2K =
      sUserAgent.indexOf("Windows NT 5.0") > -1 ||
      sUserAgent.indexOf("Windows 2000") > -1;
    if (isWin2K) return "操作系统：Win2000";
    let isWinXP =
      sUserAgent.indexOf("Windows NT 5.1") > -1 ||
      sUserAgent.indexOf("Windows XP") > -1;
    if (isWinXP) return "操作系统：WinXP";
    let isWin2003 =
      sUserAgent.indexOf("Windows NT 5.2") > -1 ||
      sUserAgent.indexOf("Windows 2003") > -1;
    if (isWin2003) return "操作系统：Win2003";
    let isWinVista =
      sUserAgent.indexOf("Windows NT 6.0") > -1 ||
      sUserAgent.indexOf("Windows Vista") > -1;
    if (isWinVista) return "操作系统：WinVista";
    let isWin7 =
      sUserAgent.indexOf("Windows NT 6.1") > -1 ||
      sUserAgent.indexOf("Windows 7") > -1;
    if (isWin7) return "操作系统：Win7";
    let isWin8 =
      sUserAgent.indexOf("windows nt6.2") > -1 ||
      sUserAgent.indexOf("Windows 8") > -1;
    if (isWin8) return "操作系统：Win8";
    let isWin10 =
      sUserAgent.indexOf("Windows NT 10.0") > -1 ||
      sUserAgent.indexOf("Windows 10") > -1;
    if (isWin10) return "操作系统：Win10";
  }
  return "其他";
};

const digits = function () {
  let sUserAgent = navigator.userAgent;
  let is64 = sUserAgent.indexOf("Win64") > -1;
  let is32 = sUserAgent.indexOf("Win32") > -1;
  if (is64) {
    return "64位";
  } else if (is32) {
    return "32位";
  } else {
    return "";
  }
};
// 检查是不是360
const _mime = function (option, value) {
  var mimeTypes = navigator.mimeTypes;
  for (var mt in mimeTypes) {
    if (mimeTypes[mt][option] === value) {
      return true;
    }
  }
  return false;
};
// 获取浏览器厂商 和 版本
const getManufacturer = function () {
  let ua = navigator.userAgent.toLocaleLowerCase();
  if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
    return "IE";
  } else if (ua.match(/firefox/) != null) {
    return "火狐";
  } else if (ua.indexOf("bidubrowser") > -1) {
    let index = ua.indexOf("bidubrowser");
    let s = ua.slice(index, -1);
    let indexEnd = s.indexOf(" ");
    let indexVersion = s.slice(0, indexEnd).indexOf("/");
    let v = s.slice(0, indexEnd).substring(indexVersion + 1);
    return `百度 v<${v}> `;
  } else if (ua.match(/opera/) != null) {
    return "欧朋";
  } else if (ua.indexOf("ubrowser") > -1) {
    let index = ua.indexOf("ubrowser");
    let s = ua.slice(index, -1);
    let indexEnd = s.indexOf(" ");
    let indexVersion = s.slice(0, indexEnd).indexOf("/");
    let v = s.slice(0, indexEnd).substring(indexVersion + 1);
    return `UC v<${v}> `;
  } else if (ua.match(/metasr/) != null) {
    return "搜狗";
  } else if (
    ua.match(/tencenttraveler/) != null ||
    ua.match(/qqbrowse/) != null
  ) {
    let index = ua.indexOf("qqbrowse") || ua.indexOf("tencenttraveler");
    let getMaxthonVersionIndex = ua.slice(index, -1).indexOf("/");
    let v = ua.slice(index, -1).slice(getMaxthonVersionIndex + 1, -1);
    return `QQ v<${v}> `;
  } else if (ua.match(/maxthon/) != null) {
    let index = ua.indexOf("maxthon");
    let getMaxthonVersionIndex = ua.slice(index, -1).indexOf("/");
    let v = ua.slice(index, -1).slice(getMaxthonVersionIndex + 1, -1);
    return `遨游 v<${v}> `;
  } else if (ua.match(/chrome/) != null) {
    var is360 = _mime("type", "application/vnd.chromium.remoting-viewer");
    if (is360) {
      return "360";
    } else {
      return "chrome";
    }
  } else if (ua.match(/safari/) != null) {
    return "Safari";
  } else {
    return "其他";
  }
};
export { getBrowserInfo, detectOS, digits, getManufacturer };
